package com.quectel.cms.controller.device;


import com.quectel.cms.controller.BaseController;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.global.annotation.RequireTenants;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.device.dto.DeviceVendorDto;
import com.quectel.core.module.device.service.DeviceVendorService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-06-03 09:55:24
 */
@RestController
@RequestMapping("deviceVendor")
@Tag(name = "DeviceVendorController", description = "设备厂商相关api")
public class DeviceVendorController extends BaseController {

    @DubboReference
    private DeviceVendorService deviceVendorService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "deviceVendor:list")
    @Operation(summary = "设备厂商的分页查询列表")
    public Response<Page<DeviceVendorDto>> list(@RequestParam Map<String, Object> params) {
        Request request = Request.configParams(params).like("code");
        List<DeviceVendorDto> list = new ArrayList<>();
        int total = deviceVendorService.queryTotal(params);
        if (total > 0) {
            list = deviceVendorService.queryList(params);
        }
        Page<DeviceVendorDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<DeviceVendorDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "deviceVendor:info")
    @Operation(summary = "查询单条的设备厂商记录")
    public Response<DeviceVendorDto> info(@PathVariable("id") Long id) {

        DeviceVendorDto deviceVendorDto = deviceVendorService.selectById(id);

        return Response.<DeviceVendorDto>ok().wrap(deviceVendorDto);
    }

    /**
     * 信息
     */
    @GetMapping("getVendorByCode/{code}")
    @Operation(summary = "通过code查询设备厂商记录")
    public Response<DeviceVendorDto> getVendorByCode(@PathVariable("code") String code) {


        DeviceVendorDto deviceVendorDto = deviceVendorService.selectByCode(code);

        return Response.<DeviceVendorDto>ok().wrap(deviceVendorDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "deviceVendor:edit")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "新增设备厂商记录")
    @Log(express = "新增设备厂商")
    public Response<Object> save(@RequestBody @Validated DeviceVendorDto deviceVendorDto) {

        DeviceVendorDto dbValue = deviceVendorService.selectByCode(deviceVendorDto.getCode());
        if (dbValue != null) {
            return Response.error("该厂商编码已存在");
        }

        //填充基础数据
        fillSaveCommonData(deviceVendorDto);

        deviceVendorService.save(deviceVendorDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "deviceVendor:edit")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "修改单条设备厂商记录")
    @Log(express = "编辑设备厂商")
    public Response<Object> update(@RequestBody @Validated DeviceVendorDto deviceVendorDto) {

        //填充基础数据
        fillEditCommonData(deviceVendorDto);

        deviceVendorService.updateById(deviceVendorDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "deviceVendor:delete")
    @RequireTenants(values = SysConstants.SYS_TOP_TENANT_CODE)
    @Operation(summary = "批量删除存在的设备厂商记录")
    @Log(express = "删除出设备厂商")
    public Response<Object> delete(@RequestBody Long[] ids) {

        deviceVendorService.deleteBatch(ids);

        return Response.ok();
    }
}
